package cn.edu.fzu.homemaking.util;

import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.domain.entity.User;
import cn.edu.fzu.homemaking.exception.ServiceException;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class UserInfoUtil {

    public static User getUserInfo(HttpServletRequest request, boolean allowNull) {
        if (Objects.equals(SessionConstant.USER, request.getSession().getAttribute(SessionConstant.USER_TYPE))) {
            return (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        }
        if (!allowNull) {
            throw new ServiceException("非法访问");
        }
        return null;
    }
}
